package  
{
	import flash.display.BitmapData;
	import flash.text.engine.JustificationStyle;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Emitter;
	import net.flashpunk.graphics.Graphiclist;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Sfx;
	import net.flashpunk.utils.Ease;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	/**
	 * ...
	 * @author Edward Russell II
	 */
	public class Player extends Entity 
	{
		
		//this creates the weapon we're going to be using
		public static var gun:Weapon = new Weapon(GC.GFX_BULLET, 10, 50, 2, 1);
		
		//Declare the variables we're going to use in the class
		public var image:Image = new Image(GC.GFX_PLAYER);
		protected var playerDeath:Sfx = new Sfx(GC.SFX_PLAYER_DEATH);
		protected var timer:Number = 0;
		protected var emitter:Emitter;
		
		public function Player(_x:int = 0,_y:int = 0) 
		{
			//set inital player start
			x = _x;
			y = _y;
			
			gun.scale = .4
			
			//scale the player image to 40%
			image.scale = .4;
			layer = -3
			//set up the hitbox and the type
			setHitbox(image.scaledWidth, image.scaledHeight);
			type = "player";
			
			//create a new emitter object that we're going to use for explosions
			emitter:emitter = new Emitter(new BitmapData(24,24,false,0x222222),24,24)
			emitter.relative = false;
			emitter.newType("explode", [0]);
			emitter.setAlpha("explode", 1, 0);
			emitter.setMotion("explode", 0, 50, 2, 360, -40, -.05, Ease.quadOut);
			
			//set the image for the player as the image and the emitter
			graphic = new Graphiclist(image, emitter);

		}
		
		override public function added():void 
		{
			GV.FIRST_PLAYER = Player(FP.world.typeFirst("player"));
			FP.world.getType("player", GV.PLAYER_LIST);
			super.added();
		}
		
		override public function update():void 
		{
			
			//as long as it isn't game over...
			if (!GV.GAME_OVER)
			{
				//control the movement
				if (Input.check(Key.RIGHT)) x += GC.PLAYER_SPEED * FP.elapsed;
				if (Input.check(Key.LEFT)) x -= GC.PLAYER_SPEED * FP.elapsed;
				if (Input.check(Key.DOWN)) y += GC.PLAYER_SPEED * FP.elapsed;
				if (Input.check(Key.UP)) y -= GC.PLAYER_SPEED * FP.elapsed;
				
				//flip the image to the correct orientation
				if (Input.pressed(Key.LEFT)) image.flipped = true; 
				if (Input.pressed(Key.RIGHT)) image.flipped = false;
					
				//if the user presses space, call the weapons fire() function
				if (Input.check(Key.SPACE)) gun.fire(x + width, y + halfWidth);
				
				//cool down the gun when not firing
				if (!Input.check(Key.SPACE)) gun.cooldown();
				
			}
			
			//if we collide into an enemy where we're currently positioned call the die() function
			//if (collide("enemy", x, y)) die();
			
			//Did you just run in to a wall? Then die!
			//if (collide("level", x, y)) die();
			
			if (collideTypes(["level", "enemy"], x, y)) die();
			
			//clamp the player to the viewable screen
			clampHorizontal(FP.camera.x, FP.camera.x + FP.screen.width);
			clampVertical(FP.camera.y, FP.camera.y + FP.screen.height);
			
			super.update();
		}
		
		public function die():void
		{
			//If the game isn't over yet
			if (!GV.GAME_OVER)
			{
				//set the game over flag
				GV.GAME_OVER = true;
				
				//set the player so he can't be seen or run into by any other enemies
				collidable = false;
				image.visible = false;
				
				//set the gun heat to 0
				gun.currentHeat = 0;
				
				//play the death sound effect
				playerDeath.play(1, 0);
				
				//Here we loop from 0 to our explosion size emitting a particle each time
				for (var i:uint = 0; i < GC.EXPLOSION_SIZE; i++)
				{
					//emit a single particle
					emitter.emit("explode", x + width / 2, y + height / 2);
				}
				
				//delete the player once all of the particles have disappeared
				if (!collidable && emitter.particleCount == 0) world.remove(this);
			}
		}
	}

}